package app.caihan.wanandroid.ui.web

import android.content.Context
import android.content.Intent
import android.os.Bundle
import app.caihan.scframe.common.loadinghelper.ToolbarConfig
import app.caihan.scframe.common.loadinghelper.ToolbarType
import app.caihan.scframe.databind.DataBindingConfig
import app.caihan.scframe.util.AgentwebHelper
import app.caihan.wanandroid.BR
import app.caihan.wanandroid.R
import app.caihan.wanandroid.base.WanBaseActivity
import app.caihan.wanandroid.databinding.ActivityBrowserUiBinding

class BrowserUi : WanBaseActivity<BrowserUiModel>() {

    companion object {
        const val TITLE = "title"
        const val URL = "url"

        fun startArticleDetail(content: Context, url: String, title: String) {
            val intent = Intent().apply {
                setClass(content, BrowserUi::class.java)
                putExtra(URL, url)
                putExtra(TITLE, title)
            }
            content.startActivity(intent)
        }
    }

    private val webViewGroup by lazy { (getBinding() as ActivityBrowserUiBinding).webcontent }
    private val agentWebHelper: AgentwebHelper by lazy { AgentwebHelper(this, webViewGroup) }

    private val webTitle by lazy { intent.getStringExtra(TITLE) }
    private val webUrl by lazy { intent.getStringExtra(URL) }

    override fun useBaseToolbar(): ToolbarConfig {
        return ToolbarConfig(
            ToolbarType.TOOLBAR,
            webTitle,
            onBackClick = this::onNavBackClick
        )
    }

    override fun onNavBackClick() {
        agentWebHelper.onBack {
            finish()
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_browser_ui
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(BR.vm)
    }

    override fun initView(savedInstanceState: Bundle?) {
    }


    override fun createObserver() {
        agentWebHelper.loadUrl(webUrl)
    }
}